package com.example.liaotian.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.liaotian.Bean.Tra;
import com.example.liaotian.Bean.User;
import com.example.liaotian.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 二手交易发帖页面
 */
public class PushTraContentActivity extends AppCompatActivity {

    private Button push;
    private EditText title;
    private EditText content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_content);

        initView();
        initClick();
    }

    private void initClick() {

        push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (title.getText().toString().isEmpty()){
                    Toast.makeText(PushTraContentActivity.this,"标题不能够为空！",Toast.LENGTH_SHORT).show();
                }else if (content.getText().toString().isEmpty()){
                    Toast.makeText(PushTraContentActivity.this,"内容不能够为空！",Toast.LENGTH_SHORT).show();
                }else {
                    User user = BmobUser.getCurrentUser(User.class);//获取到当前正在登陆的用户的数据
                    Tra tra = new Tra();

                    tra.setNikename(user.getNickName());
                    tra.setContent(content.getText().toString());
                    tra.setTitle(title.getText().toString());
                    tra.setAuthor(user);

                    tra.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null){
                                content.setText("");
                                title.setText("");

                                Toast.makeText(PushTraContentActivity.this,"发布成功！",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(PushTraContentActivity.this,"发布失败！",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });


    }

    private void initView() {

        push = findViewById(R.id.push);
        title = findViewById(R.id.titles);
        content = findViewById(R.id.contents);
    }
}
